<?php
/**
 * @author Kien Dang
 * @email dangtrungkien89@gmail.com
 * */
class GetData{
    var $module;//Module cần chèn
    var $link;//Link data
    var $rules;//Luật lấy data;
    var $tag;//Thẻ lấy
    var $output;//Đầu ra của data
    public function __construct(){
        
    }
    public function SetModule($module){
        $this->module = $module;
    }
    public function GetModule(){
        return $this->module;
    }
    public function SetLink($link){
        $this->link = $link;
    }
    public function SetTag($tag){
        $this->tag = $tag;
    }
    public function SetRules($rules){
        $this->rules = $rules;
    }
    public function Output(){
        //Proccessing
        return $this->output;
    }
}

class GetLinksTinhTe extends GetData {
    var $module = 'lay link infographic tinh te';
    var $fs_table = 'get_link';
    public function __construct(){
        $this->link = 'http://www.tinhte.vn/tags/infographic/';
    }
    protected function Proccess(){
        if($this->link && $this->rules){
            //lấy dom về
            $html = file_get_html($this->link);
            $the_links = array();
            $num = 0;
            foreach($html->find($this->rules) as $element){
                $the_links[$num] = $element->href;
                $num++;
            }
            unset($html);
            return $the_links;
        }
        else return false;
    }
    public function Output(){
        $this->output = $this->Proccess();
    }
    public function SetLinks(){
        if($this->output){
            foreach($this->output as $val_link) {
                $link_check = md5($val_link);
                $db_check = new db_query('SELECT lin_check FROM '.$this->fs_table.' WHERE lin_check = "'.$link_check.'"');
                if(!mysql_num_rows($db_check->result)){
                    $db_insert = new db_execute('
                                INSERT INTO '.$this->fs_table.'(
                                    lin_value,lin_check,lin_law_id
                                ) VALUES(
                                    "'.$val_link.'",
                                    "'.$link_check.'",
                                    1
                                )
                                ');
    
                    unset($db_insert);
                }
            }
        }
    }
}

class chiTietTinhTe extends GetData {
    var $module = 'chiTietTinhTe';
    var $fs_table = 'get_news';
    
    public function __construct($link_tt){
        $this->link = $link_tt;     
    }
    protected function Proccess(){
        if($this->link && $this->rules){
            //lấy dom về
            $html = file_get_html($this->link);
            $chiTiet = array();
            foreach($html->find($this->rules['main']) as $element){
                foreach($element->find($this->rules['title']) as $title){
                    $chiTiet['title'] = $title->innertext();
                }
                foreach($element->find($this->rules['date']) as $detail){
                    $chiTiet['date'] = $detail->innertext();
                }
                foreach($element->find($this->rules['detail']) as $detail){
                    $chiTiet['detail'] = $detail->innertext();
                }
            }
            unset($html);
            return $chiTiet;
        }
        else return false;
    }
    public function Output(){
        $this->output = $this->Proccess();
    }
    public function SetChitiet(){
        if($this->output){
            $db_check = new db_query('SELECT gne_title FROM '.$this->fs_table.' WHERE gne_type = 1 AND gne_link_md5 = "'.md5($this->link).'"');
            if(!mysql_num_rows($db_check->result)){
                global $gne_title, $gne_date, $gne_detail, $gne_link_md5, $gne_source, $gne_type;
                $gne_title = $this->output['title'];
                $gne_date = $this->output['date'];
                $gne_detail = $this->output['detail'];
                $gne_link_md5 = md5($this->link);
                $gne_source = 'Tinh Tế';
                $gne_type = 1;
           
                $myform = new generate_form();
                $myform->removeHTML(0);
                $myform->addTable('get_news');
                $myform->add('gne_title','gne_title',0,1,'');
                $myform->add('gne_date','gne_date',0,1,'');
                $myform->add('gne_detail','gne_detail',0,1,'');
                $myform->add('gne_link_md5','gne_link_md5',0,1,'');
                $myform->add('gne_source','gne_source',0,1,'');
                $myform->add('gne_type','gne_type',1,1,0);
                $db_insert	=	new db_execute($myform->generate_insert_SQL());
                unset($db_insert);
            }
        }
    }
}

class GetLinksQTM extends GetData {
    var $modules = 'lay link quan tri mang';
    var $fs_table = 'get_link';
    public function __construct(){
        parent::__construct();
    }
    protected function Proccess() {
        if($this->link && $this->rules) {
            $html = file_get_html($this->link);
            $the_links = array();
            $num = 0;
            foreach($html->find($this->rules) as $element) {
                $the_links[$num] = $element->href;
                $num++;
            }
            return $the_links;
        }
        else return false;
    }
    public function Output() {
        $this->output = $this->Proccess(); 
    }
    public function SetLinks(){
        if($this->output){
            foreach($this->output as $val_link) {
                $link_check = md5($val_link);
                $db_check = new db_query('SELECT lin_check FROM '.$this->fs_table.' WHERE lin_check = "'.$link_check.'"');
                if(!mysql_num_rows($db_check->result)){
                    $db_insert = new db_execute('
                                INSERT INTO '.$this->fs_table.'(
                                    lin_value,lin_check,lin_law_id
                                ) VALUES(
                                    "'.$val_link.'",
                                    "'.$link_check.'",
                                    2
                                )
                                ');
    
                    unset($db_insert);
                }
            }
        }
    }
}

class chiTietQTM extends GetData {
    var $module = 'chiTietQTM';
    var $fs_table = 'get_news';
    
    public function __construct($link_qtm){
        $this->link = $link_qtm;     
    }
    protected function Proccess(){
        if($this->link && $this->rules){
            //lấy dom về
            $html = file_get_html($this->link);
            $chiTiet = array();;
            foreach($html->find($this->rules['main']) as $element){
                foreach($element->find($this->rules['title']) as $title){
                    $chiTiet['title'] = $title->innertext();
                }
                foreach($element->find($this->rules['date']) as $detail){
                    $chiTiet['date'] =$detail->first_child()->innertext();
                }
                foreach($element->find($this->rules['detail']) as $detail){
                    $chiTiet['detail'] = $detail->innertext();
                }
            }
            unset($html);
            return $chiTiet;
        }else return false;
    }
    public function Output(){
        $this->output = $this->Proccess();
    }
    public function SetChitiet(){
        if($this->output){
            $db_check = new db_query('SELECT gne_title FROM '.$this->fs_table.' WHERE gne_type = 2 AND gne_link_md5 = "'.md5($this->link).'"');
            if(!mysql_num_rows($db_check->result)){
                global $gne_title, $gne_date, $gne_detail, $gne_link_md5, $gne_source, $gne_type;
                $gne_title = trim($this->output['title']);
                $gne_date = substr(trim(removeHTML($this->output['date'])), -10);
                $gne_detail = $this->output['detail'];
                $gne_link_md5 = md5($this->link);
                $gne_source = 'Quản trị mạng';
                $gne_type = 2;
           
                $myform = new generate_form();
                $myform->removeHTML(0);
                $myform->addTable('get_news');
                $myform->add('gne_title','gne_title',0,1,'');
                $myform->add('gne_date','gne_date',0,1,'');
                $myform->add('gne_detail','gne_detail',0,1,'');
                $myform->add('gne_link_md5','gne_link_md5',0,1,'');
                $myform->add('gne_source','gne_source',0,1,'');
                $myform->add('gne_type','gne_type',1,1,0);
                $db_insert	=	new db_execute($myform->generate_insert_SQL());
                unset($db_insert);
            }
        }
    }
}

?>